<?php 
class Md_User extends WdkModel
{
	public function __construct()
	{
		parent::__construct();
		$this->_init();
	}
	
	private function _init()
	{
		$this->table	= 'user';
		$this->order	= array("`user_seq` DESC");
	}
	public function count($where=array())
	{
		$where = (array) $where;
		$sql = sprintf(	"SELECT
							COUNT(*) AS `cnt`
						FROM `{$this->table}`
						%s"
						, (count($where) > 0 ? ' WHERE '.implode(' AND ', $where) : '')
					);
		$this->dbo->execute($sql);
		if($this->dbo->numRows() > 0)
		{
			$row =$this->dbo->row();
			return $row['cnt'];
		}
		throw new NoRecordException();
	}
	public function getList($fields=array('*'), $where=array(), $order=array())
	{
		$fields = (array) $fields;
		$where = (array) $where;
		$order = array_merge($this->order, (array) $order);
		$this->setOrder($order);
		foreach($fields as $idx=>$field)
		{
			$fields[$idx] = "`u`.".$field;
		}
		
		$sql = sprintf(	"SELECT 
							%s
							, `ug`.`user_group_name`, `ug`.`user_group_level`, `ug`.`user_group_icon`
							, IFNULL((SELECT SUM(`user_point_amount`) FROM `user_point` AS `up` WHERE `u`.`user_seq` = `up`.`fk_user_seq`), 0) AS `user_point`
							, (SELECT MAX(`user_login_time`) FROM `user_login` AS `ul` WHERE `ul`.`fk_user_seq` = `u`.`user_seq`) AS `user_login_time`
						FROM `{$this->table}` AS `u`
						LEFT JOIN `user_group` as `ug` ON (`u`.`fk_user_group_seq` = `ug`.`user_group_seq`)
						%s %s %s %s"
						, (count($fields) > 0 ? implode(',', $fields) : '')
						, (count($where) > 0 ? 'WHERE '.implode(' AND ', $where) : '')
						, (count($this->group) > 0 ? 'GROUP BY '.implode(', ', $this->group) : '')
						, (count($order) > 0 ? 'ORDER BY '.implode(', ', $order) : '')
						, (strlen($this->limit) > 0) ? 'LIMIT '.$this->limit : ''
					);
		$this->dbo->execute($sql);
		if($this->dbo->numRows() > 0)
		{
			return $this->dbo->rows();    		
		}
		throw new NoRecordException();
	}
	
	public function getData($fields=array('*'), $where=array())
	{
		$fields = (array) $fields;
		$where = (array) $where;
		foreach($fields as $idx=>$field)
		{
			$fields[$idx] = "`u`.".$field;
		}
		$sql = sprintf(	"SELECT 
							%s
							, `ug`.`user_group_name`, `ug`.`user_group_level`, `ug`.`user_group_icon`
							, IFNULL((SELECT SUM(`user_point_amount`) FROM `user_point` AS `up` WHERE `u`.`user_seq` = `up`.`fk_user_seq`), 0) AS `user_point`
							, (SELECT MAX(`user_login_time`) FROM `user_login` AS `ul` WHERE `ul`.`fk_user_seq` = `u`.`user_seq`) AS `user_login_time`
						FROM `{$this->table}` AS `u`
						LEFT JOIN `user_group` as `ug` ON (`u`.`fk_user_group_seq` = `ug`.`user_group_seq`)
						%s LIMIT 0,1 "
						, (count($fields) > 0 ? implode(',', $fields) : '')
						, (count($where) > 0 ? 'WHERE '.implode(' AND ', $where) : '')
					);
		$this->dbo->execute($sql);
		if($this->dbo->numRows() > 0)
		{
			return $this->dbo->row();    		
		}
		throw new NoRecordException();
	}
		
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2011. 4. 18
	 */
	public function cLoad($user_seq)
	{
		return $this->getData("*", "`u`.`user_seq` = {$this->escape($user_seq)}");
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2011. 4. 18
	 */
	public function cLoadByLoginId($user_login_id)
	{
		return $this->getData("*", "`u`.`user_login_id` = '{$this->escape($user_login_id)}'");
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2011. 4. 18
	 */
	public function cLoadByEmail($user_email)
	{
		return $this->getData("*", "`u`.`user_email` = '{$this->escape($user_email)}'");
	}
	
	/**
	 *	Select query for Classes..
	 *  @author	kinuz
	 *  @since	2011. 4. 18
	 */
	public function cLoadByNick($user_nick)
	{
		return $this->getData("*", "`u`.`user_nick` = '{$this->escape($user_nick)}'");
	}
}